### effecacy and trust on AP

rm(list=ls())
library(tidyverse)
library(ggpubr)
library(here)
here::i_am("dot_code_figs/fig-5.R")

A24 <-  read_csv("dot_code_figs/ANES files/anes_timeseries_2024_csv_20250430.csv") #2024 

anes <- A24 %>% #2024 ANES
  mutate(year = 2024) %>%     
  select(weight_post_full =  V240107b,   # weights, Post full ANES sample (FTF + panel + web + PAPI)
         weight_full =   V240107a, # 
         FT_dem = V241166,
         FT_rep = V241167,
         pid =  V241227x,
         mode = V240002a, #only want 1 (FTF) or 2 (web)
         trust_gov = V241229, #trust in gov
         pol_complicat = V242202, #internal efficacy/politics too complicated
         pol_understand = V242203,
         harris_trump_vote = V241049,
         year) %>%
  subset(pid != 4 & pid > 0)%>% #drop pure independents and NAs
  #  subset(harris_trump_vote==1 | harris_trump_vote==2)%>% #harris, trump
  subset(FT_dem >= 0 & FT_rep >=0)%>%
  #  subset(trust_gov > 0) %>%
  #  subset(pol_complicat > 0) %>%
  sapply(as.numeric) %>% data.frame()

#create pid dummy
anes$pid_dum <- ifelse(anes$pid < 4, "Democrat", "Republican")

#create voted harris dummy
anes$voted_harris <- ifelse(anes$harris_trump_vote==1, 1,0)


#create inparty and outparty
anes$inparty <- ifelse(anes$pid_dum=="Democrat", "Democrat", "Republican")
anes$outparty <- ifelse(anes$pid_dum=="Democrat", "Republican", "Democrat")

#link FTs to in and out_party
anes$inparty_FT <- ifelse(anes$pid_dum=="Democrat", anes$FT_dem, anes$FT_rep)
anes$outparty_FT <- ifelse(anes$pid_dum=="Democrat", anes$FT_rep, anes$FT_dem)


###caclulate AP as IN_FT - OUT_FT
anes$AP <- anes$inparty_FT - anes$outparty_FT

anes$ID <- seq(1, nrow(anes))



################### Now Begin the Drops
anes1 <- anes %>%
  subset(trust_gov > 0) 

trust_al_most <-anes1 %>%
  subset(trust_gov==1 | trust_gov==2)

AP_rep <- NULL
SE_rep <- NULL
AP_dem <- NULL
SE_dem <- NULL
perc <- NULL

for (i in 0:100) {
  n <- round(805*(i*0.01), digits = 0)
  drops_trust <- sample(trust_al_most$ID, size=n, replace = FALSE)
  
  #drop them
  anes_trust_drops <- anes1[!anes1$ID %in% drops_trust,]
  
  reps <- anes_trust_drops%>%
    subset(pid_dum=="Republican")
  
  dems <- anes_trust_drops%>%
    subset(pid_dum=="Democrat")
  
  trust_drop_AP_rep <- reps %>%
    select(AP, pid_dum, year, weight_post_full) %>%
    drop_na()%>%
    summarise(weighted_av_ap = weighted.mean(AP, weight_post_full, na.rm=TRUE),
              sd = sd(AP, na.rm=TRUE),
              se = sd / sqrt(n()))
  
  AP_rep[i] <- trust_drop_AP_rep$weighted_av_ap#always Dem, Rep
  SE_rep[i] <- trust_drop_AP_rep$se
  
  
  
  trust_drop_AP_dem <- dems %>%
    select(AP, pid_dum, year, weight_post_full) %>%
    drop_na()%>%
    summarise(weighted_av_ap = weighted.mean(AP, weight_post_full, na.rm=TRUE),
              sd = sd(AP, na.rm=TRUE),
              se = sd / sqrt(n()))
  AP_dem[i] <- trust_drop_AP_dem$weighted_av_ap#always Dem, Rep
  SE_dem[i] <- trust_drop_AP_dem$se
  
  perc[i] <- i
}

##now combine

republicans_drops_trust <- as.data.frame(cbind(AP_rep, SE_rep,perc)) %>%
  mutate(party="Republican") %>%
  rename(AP = AP_rep, SE=SE_rep)

democrats_drops_trust <- as.data.frame(cbind(AP_dem, SE_dem, perc)) %>%
  mutate(party="Democrat") %>%
  rename(AP = AP_dem, SE=SE_dem)

AP_drops_trust <- rbind(republicans_drops_trust, democrats_drops_trust)



##calculate undropped AP with the same weights
AP_undropped <- anes1 %>%
  select(pid_dum, weight_post_full, AP) %>%
  drop_na()%>%
  group_by(pid_dum) %>%
  summarise(weighted_av_ap = weighted.mean(AP, weight_post_full, na.rm=TRUE),
            sd = sd(AP, na.rm=TRUE),
            se = sd / sqrt(n()))

##add undropped to AP_drops_trust
dem_row<-data.frame(AP_undropped$weighted_av_ap[1], AP_undropped$se[1], 0, AP_undropped$pid_dum[1])
names(dem_row) <- c("AP", "SE", "perc", "party")

rep_row<-data.frame(AP_undropped$weighted_av_ap[2], AP_undropped$se[2], 0, AP_undropped$pid_dum[2])
names(rep_row) <- c("AP", "SE", "perc", "party")

AP_drops_trust <- rbind(AP_drops_trust, dem_row, rep_row)

##make fig

a<- ggplot(AP_drops_trust, aes(x=perc, y=AP, color=party)) +
  geom_point() +
  geom_line() +
  theme_bw() +
  scale_color_manual(values=c("black", "grey")) +
  geom_errorbar(aes(ymin=AP-(SE*1.96), ymax=AP+(SE*1.96))) +
  scale_y_continuous(limits=c(0,100)) +
  scale_x_continuous(breaks=c(0,10,20,30,40,50,60,70,80,90,99)) +
  theme(legend.position = "bottom", 
        legend.background = element_rect(color = NA, fill = NA))+
  labs(x="Percent of High Trust Respondents Removed",
       y="Affective Polarization",
       caption="ANES 2024",
       subtitle = "A",
       color="")
a


anes3 <- anes %>%
  subset(pol_understand > 0)

pol_understand <- anes3 %>%
  subset(pol_understand==1 | pol_understand==2)


AP_rep <- NULL
SE_rep <- NULL
AP_dem <- NULL
SE_dem <- NULL
perc <- NULL

for (i in 0:100) {
  n <- round(1736*(i*0.01), digits = 0)
  drops_und <- sample(pol_understand$ID, size=n, replace = FALSE)
  
  #drop them
  anes_und_drops <- anes3[!anes3$ID %in% drops_und,]
  
  reps <- anes_und_drops%>%
    subset(pid_dum=="Republican")
  
  dems <- anes_und_drops%>%
    subset(pid_dum=="Democrat")
  
  und_drop_AP_rep <- reps %>%
    select(AP, pid_dum, year, weight_post_full) %>%
    drop_na()%>%
    summarise(weighted_av_ap = weighted.mean(AP, weight_post_full, na.rm=TRUE),
              sd = sd(AP, na.rm=TRUE),
              se = sd / sqrt(n()))
  
  AP_rep[i] <- und_drop_AP_rep$weighted_av_ap#always Dem, Rep
  SE_rep[i] <- und_drop_AP_rep$se
  
  
  
  und_drop_AP_dem <- dems %>%
    select(AP, pid_dum, year, weight_post_full) %>%
    drop_na()%>%
    summarise(weighted_av_ap = weighted.mean(AP, weight_post_full, na.rm=TRUE),
              sd = sd(AP, na.rm=TRUE),
              se = sd / sqrt(n()))
  AP_dem[i] <- und_drop_AP_dem$weighted_av_ap#always Dem, Rep
  SE_dem[i] <- und_drop_AP_dem$se
  
  perc[i] <- i
}

republicans_drops_und <- as.data.frame(cbind(AP_rep, SE_rep, perc)) %>%
  mutate(party="Republican") %>%
  rename(AP = AP_rep, SE=SE_rep)

democrats_drops_und <- as.data.frame(cbind(AP_dem, SE_dem, perc)) %>%
  mutate(party="Democrat") %>%
  rename(AP = AP_dem, SE=SE_dem)

AP_drops_und <- rbind(republicans_drops_und, democrats_drops_und)


###add undropped

AP_drops_und <- rbind(AP_drops_und, dem_row, rep_row)


b<-ggplot(AP_drops_und, aes(x=perc, y=AP, color=party)) +
  geom_point() +
  geom_line() +
  theme_bw() +
  scale_color_manual(values=c("black", "grey")) +
  geom_errorbar(aes(ymin=AP-(SE*1.96), ymax=AP+(SE*1.96))) +
  scale_y_continuous(limits=c(0,100)) +
  scale_x_continuous(breaks=c(0,10,20,30,40,50,60,70,80,90,99)) +
  theme(legend.position = c(0.8,0.3), 
        legend.background = element_rect(color = NA, fill = NA))+
  labs(x="Percent of High Efficacy Respondents Removed",
       y="Affective Polarization",
       subtitle = "B",
      caption="ANES 2024",
       color="")
b

png(file = "dot_code_figs/figs/figure-5.png",
    height = 6,
    width = 10,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(a,b,
                nrow = 1, ncol = 2, common.legend = T, legend = "bottom"))  

dev.off()

